import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controllers/profile_controller.dart';
import '../widgets/profile_avatar.dart';
import '../widgets/info_item.dart';
import '../../shared/widgets/buttons/app_button.dart';

/// 个人资料视图
class ProfileView extends GetView<ProfileController> {
  const ProfileView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('个人中心'),
        centerTitle: true,
      ),
      body: Obx(() {
        if (controller.isLoading) {
          return const Center(child: CircularProgressIndicator());
        }
        return _buildProfileContent(context);
      }),
    );
  }
  
  Widget _buildProfileContent(BuildContext context) {
    return ListView(
      padding: const EdgeInsets.symmetric(vertical: 16.0),
      children: [
        // 顶部用户信息区域
        _buildUserInfoSection(),
        
        const SizedBox(height: 20),
        
        // 菜单列表 - 账户与安全
        _buildSectionTitle('账户与安全'),
        _buildMenuItem(
          icon: Icons.account_circle_outlined,
          title: '编辑个人资料',
          onTap: controller.navigateToEditProfile,
        ),
        _buildMenuItem(
          icon: Icons.lock_outline,
          title: '账户设置',
          onTap: controller.navigateToAccountSettings,
        ),
        
        const SizedBox(height: 20),
        
        // 菜单列表 - 设置
        _buildSectionTitle('设置'),
        _buildNotificationSettingItem(),
        _buildMenuItem(
          icon: Icons.privacy_tip_outlined,
          title: '隐私设置',
          onTap: controller.navigateToPrivacySettings,
        ),
        _buildMenuItem(
          icon: Icons.language,
          title: '语言设置',
          onTap: () => Get.snackbar('提示', '语言设置功能正在开发中'),
        ),
        
        const SizedBox(height: 20),
        
        // 菜单列表 - 关于与帮助
        _buildSectionTitle('关于与帮助'),
        _buildMenuItem(
          icon: Icons.help_outline,
          title: '帮助中心',
          onTap: () => Get.snackbar('提示', '帮助中心功能正在开发中'),
        ),
        _buildMenuItem(
          icon: Icons.info_outline,
          title: '关于我们',
          onTap: controller.navigateToAbout,
        ),
        _buildMenuItem(
          icon: Icons.system_update_alt,
          title: '检查更新',
          onTap: () => Get.snackbar('提示', '已是最新版本'),
        ),
        
        const SizedBox(height: 30),
        
        // 退出登录按钮
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16.0),
          child: AppButton(
            text: '退出登录',
            onPressed: controller.logout,
            backgroundColor: Colors.red[50],
            foregroundColor: Colors.red,
          ),
        ),
        
        const SizedBox(height: 20),
      ],
    );
  }

  /// 构建顶部用户信息区域
  Widget _buildUserInfoSection() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16.0),
      child: Row(
        children: [
          ProfileAvatar(
            avatarUrl: controller.userAvatarUrl,
            userName: controller.userName,
            radius: 40,
          ),
          const SizedBox(width: 16),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  controller.userName,
                  style: const TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 4),
                Text(
                  controller.userEmail,
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.grey[600],
                  ),
                ),
                const SizedBox(height: 4),
                Text(
                  '用户角色: ${controller.userRole}',
                  style: TextStyle(
                    fontSize: 12,
                    color: Colors.grey[600],
                  ),
                ),
              ],
            ),
          ),
          IconButton(
            icon: const Icon(Icons.edit_outlined),
            onPressed: controller.navigateToEditProfile,
            tooltip: '编辑资料',
          ),
        ],
      ),
    );
  }

  /// 构建菜单区域标题
  Widget _buildSectionTitle(String title) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
      child: Text(
        title,
        style: TextStyle(
          fontSize: 14,
          fontWeight: FontWeight.bold,
          color: Colors.grey[700],
        ),
      ),
    );
  }

  /// 构建通用菜单项
  Widget _buildMenuItem({
    required IconData icon,
    required String title,
    Widget? trailing,
    VoidCallback? onTap,
  }) {
    return ListTile(
      leading: Icon(icon, color: Get.theme.primaryColor.withOpacity(0.8)),
      title: Text(title, style: const TextStyle(fontSize: 16)),
      trailing: trailing ?? const Icon(Icons.chevron_right, color: Colors.grey),
      onTap: onTap,
    );
  }
  
  /// 构建通知设置项 (带开关)
  Widget _buildNotificationSettingItem() {
    return Obx(() => ListTile(
      leading: Icon(Icons.notifications_outlined, color: Get.theme.primaryColor.withOpacity(0.8)),
      title: const Text('接收通知', style: TextStyle(fontSize: 16)),
      trailing: Switch(
        value: controller.notificationsEnabled,
        onChanged: controller.toggleNotifications,
        activeColor: Get.theme.primaryColor,
      ),
      onTap: () => controller.toggleNotifications(!controller.notificationsEnabled),
    ));
  }
} 